<template>
  <div class="goods-image">
    <div
      class="large"
      :style="{
        backgroundImage: `url(${images[indexLight]})`,
        backgroundPositionX: largeLens.x + 'px',
        backgroundPositionY: largeLens.y + 'px',
      }"
      v-show="show"
    ></div>
    <div class="middle" ref="target">
      <img :src="images[indexLight]" alt="" />
      <div
        class="layer"
        v-show="show"
        :style="{
          left: littleLens.left + 'px',
          top: littleLens.top + 'px',
        }"
      ></div>
    </div>
    <ul class="small">
      <li
        v-for="(item, index) in images"
        :class="{ active: indexLight === index }"
        :key="item"
        @click="handleChangeLight(index)"
      >
        <img :src="item" alt="" />
      </li>
    </ul>
  </div>
</template>

<script>
import { ref, watch } from "vue";
import { useMouseInElement } from "@vueuse/core";

export default {
  name: "GoodsImages",
  props: {
    images: {
      type: Array,
      default: () => [],
    },
  },
  setup() {
    // 存放索引决定是哪个图片框高亮
    let indexLight = ref(0);
    // 需要监听的元素盒子
    let target = ref(null);
    let { elementX, elementY, isOutside } = useMouseInElement(target);
    // 放大镜盒子显示隐藏
    let show = ref(false);
    // 用于存储小镜片的位置
    let littleLens = ref({
      left: 0,
      top: 0,
    });
    // 用于存储大镜片的位置
    let largeLens = ref({
      x: 0,
      y: 0,
    });

    // 点击改变谁高亮
    const handleChangeLight = (index) => {
      indexLight.value = index;
    };

    // 监听 useMouseInElement 的变化
    watch([elementX, elementY, isOutside], () => {
      show.value = !isOutside.value;
      // console.log(elementX.value, elementY.value);
      // 改变小镜片的位置
      littleLens.value = {
        left: elementX.value - 100,
        top: elementY.value - 100,
      };

      // 限制不让小镜片超出图片框架
      if (elementX.value <= 100) {
        littleLens.value.left = 0;
      } else if (elementX.value >= 300) {
        littleLens.value.left = 200;
      }

      if (elementY.value <= 100) {
        littleLens.value.top = 0;
      } else if (elementY.value >= 300) {
        littleLens.value.top = 200;
      }

      // 改变大镜片的位置
      largeLens.value = {
        x: -littleLens.value.left * 2,
        y: -littleLens.value.top * 2,
      };
    });

    return {
      indexLight,
      handleChangeLight,
      target,
      show,
      littleLens,
      largeLens,
    };
  },
};
</script>

<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 500;

  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;

    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }

  .small {
    width: 80px;

    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;

      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
